package cn.wenjie.gd.test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import cn.wj.base.util.DBPool;

public class DBtest {
	private static DBPool dbpool;
	public static void main(String[] args) {
		int i=0;
		while (true) {
			i++;
			try {
				Thread.sleep(2000);
			insert(i);
			Thread.sleep(5000);
			select();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	static  {
		dbpool = DBPool.getInstance();
	}
	public static void insert(int i){
		String sql=" UPDATE USER SET email ='testemail"+i+"' , addr ='地址"+i+"'    WHERE id =3";
		Connection conn=conn = dbpool.getConnection();
		try {
			PreparedStatement ps = conn.prepareStatement(sql);
			int res = ps.executeUpdate();
			ps.close();
			dbpool.releaseConnection(conn);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	public static void select(){
		Connection conn=conn = dbpool.getConnection();
		try {
			PreparedStatement ps = conn.prepareStatement(" SELECT id,name,cardId,sex,role,point,loginName,password,email,addr,tel FROM user where id =3 ");
			ResultSet res = ps.executeQuery();
			ResultSetMetaData MDA = res.getMetaData();
			Map<String, Integer> columnLabelMap = new HashMap<String, Integer>();
			for (int i = 0; i < MDA.getColumnCount(); i++) {
				columnLabelMap.put(MDA.getColumnLabel(i + 1), MDA.getColumnType(i + 1));
			}
			
			
			while (res.next()) {
				System.out.println("id:"+res.getObject(1)+"         name:"+res.getObject(2)+"         email:"+res.getObject(9)+"     addr:"+res.getObject(10));
			}
			res.close();
			ps.close();
			dbpool.releaseConnection(conn);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
